import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

/// 下面的测试中大量使用这个结构，因此将它抽象出来
/// 输入一个body，预测出来的string和contentType，其他部分被省略
Future<void> checkResponse(
  dynamic bodyInput,
  String bodyMatch,
  String contentTypeMatch,
) async {
  // 状态码在这里不重要，因此随便传入即可
  final r = response(200, bodyInput);
  // 解码body
  final String body = await r.readAsString();
  // 预测body的值和contentType的值
  expect(body, equals(bodyMatch));
  expect(r.headers[keyContentType], equals(contentTypeMatch));
}

void main() {
  test("状态码检查", () {
    // 输入的状态码会反馈在response之中
    final response1 = response(200);
    expect(response1.statusCode, 200);
    final response2 = response(400);
    expect(response2.statusCode, 400);
  });

  test("空body", () async => await checkResponse(null, "", valueText));

  test("发送Exception错误", () async {
    final exception = Exception("一个Exception");
    await checkResponse(exception, stringify(exception), valueText);
  });
  test("发送Bug错误", () async {
    final bug = Bug("一个Bug");
    await checkResponse(bug, stringify(bug), valueText);
  });

  test("发送对象", () async {
    await checkResponse(pseudoThree, stringify(pseudoThree), valueJson);
  });

  test("发送对象列表", () async {
    await checkResponse(pseudoPeople, stringify(pseudoPeople), valueJson);
  });
  test("发送对象Map", () async {
    final map = {
      "three": pseudoThree,
      "four": pseudoFour,
    };
    await checkResponse(map, stringify(map), valueJson);
  });

  test("发送字符串", () async => await checkResponse("abc", "abc", valueText));
}
